package cn.exrick.xboot.modules.research.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;

import cn.exrick.xboot.modules.research.entity.StaffManagement;
import cn.exrick.xboot.modules.research.service.IStaffManagementService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lty
 */
@Slf4j
@RestController
@Api(tags = "教工管理管理接口")
@RequestMapping("/xboot/staffManagement")
@Transactional
public class StaffManagementController {

    @Autowired
    private IStaffManagementService iStaffManagementService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<StaffManagement> get(@PathVariable String id) {

        StaffManagement staffManagement = iStaffManagementService.getById(id);
        return new ResultUtil<StaffManagement>().setData(staffManagement);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<StaffManagement>> getAll() {

        List<StaffManagement> list = iStaffManagementService.list();
        return new ResultUtil<List<StaffManagement>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<StaffManagement>> getByPage(@ModelAttribute StaffManagement staffManagement,@ModelAttribute PageVo page) {
        QueryWrapper<StaffManagement> qw =new QueryWrapper<>();

        if (StrUtil.isNotBlank(staffManagement.getStaffId())){
             qw.like("staff_Id",staffManagement.getStaffId());
        }
        if (StrUtil.isNotBlank(staffManagement.getName())){
            qw.like("name",staffManagement.getName());
        }
        if (StrUtil.isNotBlank(staffManagement.getNickname())){
            qw.like("nickname",staffManagement.getNickname());
        }
        if (StrUtil.isNotBlank(staffManagement.getMobile())){
            qw.like("mobile",staffManagement.getMobile());
        }
        if (StrUtil.isNotBlank(staffManagement.getDepartmentName())){
            qw.like("department_Name",staffManagement.getDepartmentName());
        }

        IPage<StaffManagement> data = iStaffManagementService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<StaffManagement>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<StaffManagement> saveOrUpdate(StaffManagement staffManagement) {

        if (iStaffManagementService.saveOrUpdate(staffManagement)) {
            return new ResultUtil<StaffManagement>().setData(staffManagement);
        }
        return new ResultUtil<StaffManagement>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestParam String[] ids) {

        for (String id : ids) {
            iStaffManagementService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }
}
